package com.software.service.impl;

import com.software.bean.CustomerInfoBean;
import com.software.bean.CustomerLoginBean;
import com.software.dto.CustomerLoginDto;
import com.software.dto.CustomerRegisterDto;
import com.software.dto.CustomerReturnPwdDto;
import com.software.dto.UpdatePasswordDto;
import com.software.mapper.CustomerInfoMapper;
import com.software.mapper.CustomerLoginMapper;
import com.software.service.CustomerLoginService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.software.utils.AssertUtil;
import com.software.utils.Md5Util;
import com.software.utils.PhoneUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wyj
 * @since 2024-03-19
 */
@Service
public class CustomerLoginServiceImpl extends ServiceImpl<CustomerLoginMapper, CustomerLoginBean> implements CustomerLoginService {

    @Autowired
    private CustomerLoginMapper customerLoginMapper;
    @Autowired
    private CustomerInfoMapper customerInfoMapper;

    // 用户登录
    @Override
    public CustomerInfoBean login(CustomerLoginDto customerLoginDto) {
        //参数校验
        AssertUtil.isTrue(StringUtils.isEmpty(customerLoginDto.getAccount()), "账号不能为空");
        AssertUtil.isTrue(StringUtils.isEmpty(customerLoginDto.getPassword()), "密码不能为空");
        //根据账号查登陆表
        CustomerLoginBean customerLoginBean = customerLoginMapper.selectByAccount(customerLoginDto.getAccount());
        AssertUtil.isTrue(customerLoginBean == null, "账号不存在");
        AssertUtil.isTrue(!customerLoginBean.getPassword().equals(Md5Util.encode(customerLoginDto.getPassword())), "密码错误");
        AssertUtil.isTrue(customerLoginBean == null, "返回值异常");
        AssertUtil.isTrue(customerLoginBean.getStatus() != 1, "账号禁用");
        // 根据登陆表id 查用户信息
        CustomerInfoBean customerInfoBean = customerInfoMapper.selectByLoginId(customerLoginBean.getId());
        AssertUtil.isTrue(customerInfoBean == null, "用户信息异常");
        return customerInfoBean;
    }


    // 用户注册
    @Override
    @Transactional
    public Integer register(CustomerRegisterDto registerDto) {
        // 参数校验
        AssertUtil.isTrue(StringUtils.isEmpty(registerDto.getAccount()), "账号不能为空");
        AssertUtil.isTrue(StringUtils.isEmpty(registerDto.getPassword()), "密码不能为空");
        AssertUtil.isTrue(StringUtils.isEmpty(registerDto.getName()), "用户名不能为空");
        AssertUtil.isTrue(StringUtils.isEmpty(registerDto.getPhone()), "手机号不能为空");
        AssertUtil.isTrue(PhoneUtils.isMobile(registerDto.getPhone()) == false, "请输入正确手机号");
        // 判断账号不能重复
        CustomerLoginBean account = customerLoginMapper.selectByAccount(registerDto.getAccount());
        AssertUtil.isTrue(account != null, "账号重复");
        CustomerLoginBean customerLoginBean = new CustomerLoginBean();
        customerLoginBean.setAccount(registerDto.getAccount());
        customerLoginBean.setPassword(Md5Util.encode(registerDto.getPassword()));
        customerLoginBean.setStatus(1);
        int insert = customerLoginMapper.insert(customerLoginBean);
        AssertUtil.isTrue(insert != 1, "插入失败");

        CustomerInfoBean customerInfoBean = new CustomerInfoBean();
        customerInfoBean.setCustomerName(registerDto.getName());
        customerInfoBean.setCustomerLoginId(customerLoginBean.getId());
        customerInfoBean.setMobilePhone(registerDto.getPhone());
        //设置默认头像
        customerInfoBean.setAvatarUrl("http://localhost:8082/file/default_avatar.png");
        customerInfoBean.setRegisterTime(new Date());
        customerInfoBean.setUpdateTime(new Date());
        // 设置性别默认值 配合小程序端
        customerInfoBean.setCustomerSex(0);


        int result = customerInfoMapper.insert(customerInfoBean);
        return result == 1 ? 1 : 0;
    }

    /**
     * @description:更新密码
     * @author: wyj
     * @date: 2024/3/30 21:57
     * @param:
     * @return:
     */
    @Override
    public Integer updatePassword(UpdatePasswordDto updatePasswordDto) {
        // 参数校验
        AssertUtil.isTrue(updatePasswordDto.getCid() == null, "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(updatePasswordDto.getOldPassword()), "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(updatePasswordDto.getNewPasswordOne()), "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(updatePasswordDto.getNewPasswordTwo()), "参数异常");

        // 判断cid 是否正常
        CustomerInfoBean customerInfoBean = customerInfoMapper.selectById(updatePasswordDto.getCid());
        AssertUtil.isTrue(customerInfoBean == null, "返回值异常");
        CustomerLoginBean customerLoginBean = customerLoginMapper.selectById(customerInfoBean.getCustomerLoginId());
        AssertUtil.isTrue(customerLoginBean == null, "返回值异常");
        AssertUtil.isTrue(!customerLoginBean.getPassword().equals(Md5Util.encode(updatePasswordDto.getOldPassword())), "密码错误");
        customerLoginBean.setPassword(Md5Util.encode(updatePasswordDto.getNewPasswordOne()));
        int i = customerLoginMapper.updateById(customerLoginBean);
        return i == 1 ? 1 : 0;
    }

    /**
     * @description:修改密码
     * @author: wyj
     * @date: 2024/4/23 13:24
     * @param:
     * @return:
     */
    @Override
    public Integer returnPwd(CustomerReturnPwdDto returnPwdDto) {
        AssertUtil.isTrue(StringUtils.isEmpty(returnPwdDto.getAccount()), "账号不能为空");
        AssertUtil.isTrue(StringUtils.isEmpty(returnPwdDto.getPhone()), "手机号不能为空");
        AssertUtil.isTrue(StringUtils.isEmpty(returnPwdDto.getPassword()), "密码不能为空");
        AssertUtil.isTrue(PhoneUtils.isMobile(returnPwdDto.getPhone()) == false, "请输入正确手机号");
        CustomerLoginBean customerLoginBean = customerLoginMapper.selectByAccount(returnPwdDto.getAccount());
        AssertUtil.isTrue(customerLoginBean == null, "账号不存在");
        CustomerInfoBean customerInfoBean = customerInfoMapper.selectByLoginId(customerLoginBean.getId());
        AssertUtil.isTrue(customerInfoBean == null, "用户信息异常");
        AssertUtil.isTrue(!customerInfoBean.getMobilePhone().equals(returnPwdDto.getPhone()), "手机号不匹配");
        customerLoginBean.setPassword(Md5Util.encode(returnPwdDto.getPassword()));
        int i = customerLoginMapper.updateById(customerLoginBean);
        return i == 1 ? 1 : 0;

    }
}
